<?php
/**
 * User: hjd-yao
 * Date: 2019/5/13
 * Time: 11:12
 */

namespace App\Http\Controllers\Stats;


use App\Http\Controllers\BackendController;
use App\Models\Shop\ShopStatsModel;
use Carbon\Carbon;

class ShopStatsController extends BackendController
{
    /**
     * @title 销售趋势
     * @return JsonResponse
     */
    public function salesStats()
    {
        $list = ShopStatsModel::where('stats_date','>=',Carbon::today()->subDay(30)->timestamp)->where('store_id',0)
            ->select(['stats_date','goods_zone','sales_money'])->get();

        $dates = $sales1 = $sales2 = $sales3= $sales4 = [];
        for ($i = 0; $i < 15; $i++){
            $day = Carbon::today()->subDay(14-$i)->timestamp;
            $dates[] = Carbon::today()->subDay(14-$i)->format('Y/m/d');
            $day_sale = [];
            foreach ($list as $value){
                if($value->stats_date != $day){
                    continue;
                }
                $day_sale[$value->goods_zone] = $value->sales_money;
            }

            $sales1[] = $day_sale[1]??0;
            $sales2[] = $day_sale[2]??0;
            $sales3[] = $day_sale[3]??0;
            $sales4[] = $day_sale[4]??0;
        }
        return response()->json(['status'=>1,'data'=>[
            'dates'=>$dates,
            'sales1'=>$sales1,
            'sales2'=>$sales2,
            'sales3'=>$sales3,
            'sales4'=>$sales4,
        ]]);
    }


    /**
     * @title 销售量统计
     * @return View
     */
    public function salesCount()
    {
        $list = ShopStatsModel::where('stats_date','>=',Carbon::today()->subDay(30)->timestamp)->where('store_id',0)
            ->selectRaw("goods_zone,sum(sales_money) as sales_count")->groupBy('goods_zone')->get();
        return view('backend::stats.shop.sales',[
            'page_title'=>'销售统计',
            'count'=>array_column($list->toArray(),'sales_count','goods_zone'),
        ]);
    }
}